<template>
    <div class="app-container">
        <el-form :inline="true" ref="queryRef" label-position="right" :model="params" label-width="200px">
            <div class="form-inline">
                <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="烈度：" class="form-item" prop="intensity">
                            <el-select v-model="params.intensity" placeholder="请选择" class="form-select" clearable>
                                <el-option v-for="item in intensityOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="破坏状态：" class="form-item" prop="damage">
                            <el-input v-model="params.damage" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="维护人：" class="form-item" prop="userName">
                            <el-input v-model="params.userName" placeholder="请输入" clearable />
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="维护时间:">
                            <el-col :span="11">
                                <el-form-item prop="createStartTime">
                                    <el-date-picker clearable v-model="params.createStartTime" type="date"
                                        placeholder="开始时间" value-format="YYYY-MM-DD" :disabled-date="startDisableDate"
                                        style="width: 100%" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="1" class="text-center">
                                <span class="text-gray-500">-</span>
                            </el-col>
                            <el-col :span="11">
                                <el-form-item prop="createEndTime">
                                    <el-date-picker clearable v-model="params.createEndTime" type="date" placeholder="结束时间"
                                        value-format="YYYY-MM-DD" :disabled-date="endDisableDate" style="width: 100%" />
                                </el-form-item>
                            </el-col>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row justify="center">
                    <!-- <el-col :span="11">
                        <el-form-item label="维护人所属单位：" class="form-item" prop="maintainerUnit">
                            <el-input v-model="params.maintainerUnit" placeholder="" clearable />
                        </el-form-item>
                    </el-col> -->
                    <el-col :span="11">
                        <el-form-item label="用途：">
                            <el-col :span="11">
                                <el-form-item prop="use0">
                                    <el-select v-model="params.use0" placeholder="请选择" class="form-select"
                                        @change="useChange" clearable>
                                        <el-option v-for="item in useOptions" :key="item.value" :label="item.label"
                                            :value="item.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="1" class="text-center">
                                <span class="text-gray-500">-</span>
                            </el-col>
                            <el-col :span="11">
                                <el-form-item prop="use1">
                                    <el-select v-model="params.use1" placeholder="请选择" class="form-select" clearable>
                                        <el-option v-for="item in use1Options" :key="item.value" :label="item.label"
                                            :value="item.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="结构类型：">
                            <el-col :span="11">
                                <el-form-item prop="structureType">
                                    <el-select v-model="params.structureType" placeholder="请选择" class="form-select"
                                        @change="structureTypeChange" clearable>
                                        <el-option v-for="item in structureTypeOptions" :key="item.value"
                                            :label="item.label" :value="item.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="1" class="text-center">
                                <span class="text-gray-500">-</span>
                            </el-col>
                            <el-col :span="11">
                                <el-form-item prop="structureType1">
                                    <el-select v-model="params.structureType1" placeholder="请选择" class="form-select"
                                        clearable>
                                        <el-option v-for="item in structureType1Options" :key="item.value"
                                            :label="item.label" :value="item.value">
                                        </el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="区域：" class="form-item" prop="region">
                            <el-form-item prop="province" style="width:120px;margin-right:10px;">
                                <el-select v-model="params.province" placeholder="请选择省" @change="chooseProvince" clearable
                                    value-key="id">
                                    <el-option v-for="item in provinceData" :key="item.id" :label="item.name" :value="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item prop="city" style="width:120px;margin-right:10px;">
                                <el-select v-model="params.city" placeholder="请选择市" @change="chooseCity" clearable
                                    value-key="id">
                                    <el-option v-for="item in cityData" :key="item.id" :label="item.name" :value="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item prop="county" style="width:120px;">
                                <el-select v-model="params.county" placeholder="请选择县" @change="chooseCounty" clearable
                                    value-key="id">
                                    <el-option v-for="item in areaData" :key="item.id" :label="item.name" :value="item">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-form-item>
                    </el-col>
                    <el-col :span="11">
                        <el-form-item label="设防情况：" class="form-item" prop="fortification">
                            <el-select v-model="params.fortification" placeholder="请选择" class="form-select" clearable>
                                <el-option v-for="item in fortificationOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row justify="center">
                    <el-col :span="11">
                        <el-form-item label="建造年代：" class="form-item" prop="years">
                            <el-select v-model="params.years" placeholder="请选择" class="form-select" clearable>
                                <el-option v-for="item in yearsOptions" :key="item.value" :label="item.label"
                                    :value="item.value">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span='11'></el-col>
                </el-row>
                <el-row justify="end">
                    <el-form-item class="bottom-button">
                        <el-button type="primary" @click="onImport">导入</el-button>
                        <el-button type="primary" @click="onSearch">查询</el-button>
                        <el-button type="primary" @click="onRest">重置</el-button>
                    </el-form-item>
                </el-row>
            </div>
        </el-form>
        <div class="table-wrapper">
            <el-table class="listTable" ref="multipleTableRef" border :data="tableData" style="width: 100%"
                @selection-change="handleSelectionChange" :cell-style="{ border: '1px solid #e5ebf7' }"
                :header-cell-style="{ border: '1px solid #e5ebf7', borderLeft: 'none' }">
                <el-table-column type="selection" width="55" label="序号" />
                <el-table-column prop="number" header-align="center" align="center" label="编号" width="120">
                    <template #default="scope">
                        <el-button link type="text" size="small" @click.prevent="viewProject(scope.row)">
                            {{ scope.row.number }}
                        </el-button>
                    </template>
                </el-table-column>
                <el-table-column prop="region" header-align="center" align="center" label="区域" width="200" />
                <el-table-column prop="st" header-align="center" align="center" label="结构类型" width="160" />
                <el-table-column prop="years" header-align="center" align="center" label="建造年代" width="120" />
                <el-table-column prop="fortification" header-align="center" align="center" label="设防情况" width="85" />
                <el-table-column prop="cs" header-align="center" align="center" label="层数" width="80" />
                <el-table-column prop="userName" header-align="center" align="center" label="维护人" />
                <el-table-column prop="createTime" header-align="center" align="center" label="维护时间" width="160" />
                <el-table-column prop="intensity" header-align="center" align="center" label="烈度" />
                <el-table-column prop="damage" header-align="center" align="center" label="破坏状态" />
                <el-table-column prop="use" header-align="center" align="center" label="用途" />
                <el-table-column label="操作" fixed="right">
                    <template #default="scope">
                        <el-button type="text" size="small" @click="onDelete(scope.row)">删除</el-button>
                        <!-- <el-button type="text" size="small" @click="onModify(scope.row)">修改</el-button> -->
                    </template>
                </el-table-column>
            </el-table>
            <div class="paging">
                <el-pagination background v-model:current-page="queryPagination.pageNo" :layout="layout"
                    :page-size="queryPagination.pageSize" :total="queryPagination.total"
                    @current-change="handleCurrentChange" @size-change="handleSizeChange" />
            </div>
        </div>
    </div>
</template>

<script setup>
import { reactive, toRefs } from "vue";
// 引入接口
import { getInfo } from '@/api/login'
import { getUserId } from "@/utils/auth";
import { updateBuildings, deleteBuildingsById, getBuildingsById, queryBuildings } from "@/api/importData/buildingDamageData.js";
// 引入行政区划接口
import { getProvince, getChildArea } from "@/api/system/division";
// 引入store
import useDictStore from '@/store/modules/dict';
const dictStore = useDictStore();

// 当前实例
const { proxy } = getCurrentInstance()
// 全局路由
const router = useRouter();

const data = reactive({
    // 表单参数
    params: {
        intensity: "", //烈度
        damage: "", //破坏状态
        createStartTime: "", //维护开始时间
        createEndTime: "",//维护结束时间
        userName: "", //维护人
        structureType: "", //结构类型
        structureType1: "",
        maintainerUnit: "", //维护人所属单位
        fortification: "", //设防情况
        years: "",//建造年代
        region: "",//区域
        use0: "",//用途
        use1: "",
        province: "",//省
        city: "",//省
        county: "",//省
    },
    // 烈度
    intensityOptions: dictStore.intensityOptions || [],
    //设防情况：请选择、未设防、6度、7度、8度、9度、自设防、其他
    fortificationOptions: dictStore.fortificationOptions || [],
    // 建造年代
    yearsOptions: dictStore.yearsOptions || [],
    // 结构类型
    structureTypeOptions: dictStore.structureTypeOptions || [],
    structureType1Options: [],
    // 用途
    useOptions: [
        {
            value: '第Ⅰ类',
            label: '第Ⅰ类',
            children: [
                {
                    value: '住宅',
                    label: '住宅',
                }, {
                    value: '公寓',
                    label: '公寓',
                }, {
                    value: '学生和职工集体宿舍',
                    label: '学生和职工集体宿舍',
                }, {
                    value: 'bg',
                    label: '宾馆',
                }, {
                    value: 'zds',
                    label: '招待所',
                }, {
                    value: 'qtryjzcs',
                    label: '其他人员居住场所',
                }]
        }, {
            value: '第Ⅱ类',
            label: '第Ⅱ类',
            children: [
                {
                    value: '办公室',
                    label: '办公室',
                }, {
                    value: '生产车间',
                    label: '生产车间',

                }, {
                    value: '教室',
                    label: '教室'
                }, {
                    value: '其他工作与学习场所',
                    label: '其他工作与学习场所',
                }]
        }, {
            value: '第Ⅲ类',
            label: '第Ⅲ类',
            children: [{
                value: '除I、II类以外',
                label: '除I、II类以外'
            }]
        }
    ],
    use1Options: [],
    //表格
    tableData: [],
    //分页
    layout: " prev, pager, next, jumper, sizes, total",
    queryPagination: {
        pageNo: 1,
        pageSize: 10,
        total: 100,
    },
})
const {
    params,
    intensityOptions,
    fortificationOptions,
    yearsOptions,
    structureTypeOptions,
    structureType1Options,
    useOptions,
    use1Options,
    tableData,
    queryPagination,
    layout
} = toRefs(data);

// 行政区划
const provinceData = ref([]); //省
const cityData = ref([]); //市
const areaData = ref([]); //区、县
/** 获取省列表 */
function getProvinceData() {
    getProvince().then((response) => {
        provinceData.value = response.data;
    });
}
// 选择省份
function chooseProvince(value) {
    params.value.city = null;
    params.value.county = null;
    params.value.province = value.name;
    cityData.value = [];
    areaData.value = [];
    getChildArea(value.id).then(response => {
        cityData.value = response.data;
    });
}
// 选择城市
function chooseCity(value) {
    params.value.city = value.name;
    params.value.county = null;
    areaData.value = [];
    getChildArea(value.id).then((response) => {
        areaData.value = response.data;
    });
}
// 选择区县
function chooseCounty(value) {
    params.value.county = value.name;
}

// 开始时间可选区域控制
const startDisableDate = (time) => {
    let endDateVal = params.value.createEndTime
    if (endDateVal) {
        return time.getTime() > new Date(endDateVal).getTime()
    }
}
// 结束时间可选区域控制
const endDisableDate = (time) => {
    let beginDateVal = params.value.createStartTime
    if (beginDateVal) {
        return time.getTime() < new Date(beginDateVal).getTime()
    }
}

// 结构类型选择
const structureTypeChange = (val) => {
    const structureTypes = structureTypeOptions.value.find(item => item.value === val)
    if (structureTypes) {
        params.value.structureType1 = '';
        structureType1Options.value = structureTypes.children
    }
}
// 用途选择
const useChange = (val) => {
    const use = useOptions.value.find(item => item.value === val)
    if (use) {
        params.value.use1 = '';
        use1Options.value = use.children
    }
}

// 获取用户
function getUserInfo() {
    getInfo(getUserId()).then(response => {
        // params.value.userName = response.user.userName
        // params.value.maintainerUnit = response.user.orgName
        //params.value.createTime = parseTime(new Date())
    });
};


// 表格
const multipleTableRef = ref();
const handleSelectionChange = (val) => {
    //   multipleSelection.value = val;
    //   curTableRow.value = val[0];
};
// 分页,每页显示条数选择
const handleSizeChange = (val) => {
    // 改变每页显示的条数
    queryPagination.value.pageSize = val
    // 注意：在改变每页显示的条数时，要将页码显示到第一页
    queryPagination.value.pageNo = 1
    onSearch()
};
// 分页,当前页数选择
const handleCurrentChange = (val) => {
    queryPagination.value.pageNo = val
    onSearch()
};

// 点击编号跳转至此数据的详细信息界面
const viewProject = (row) => {
    router.push({
        path: '/disasterDataManagement/maintainData/buildVulnerabilityInfo',
        query: {
            id: row.cbhdId,
            number: row.number,
        }
    })
}
//修改
const onModify = (row) => {
    router.push({
        path: '/disasterDataManagement/maintainData/buildVulnerabilityInfo',
        query: {
            id: row.cbhdId,
            number: row.number,
            edit: true
        }
    })
}
//删除
const onDelete = (row) => {
    console.log(row)
    const query = { number: row.number }
    deleteBuildingsById(query).then((res) => {
        if (res.code === 200) {
            proxy.$modal.msgSuccess("删除成功");
            onSearch();
        }
    }).catch(err => {
        console.log(err);
    })
}

//建筑物区域特征信息维护表格查询
const onSearch = () => {
    const query = {
        ...params.value,
        page: queryPagination.value.pageNo,
        rows: queryPagination.value.pageSize
    }
    queryBuildings(query).then((res) => {
        if (res.code === 200) {
            console.log(res)
            queryPagination.value.total = res.data.total
            tableData.value = [];
            const rows = res.data.rows;
            //循环遍历之前加上非空判断
            if (rows.length >= 0) {
                rows.forEach((item) => {
                    tableData.value.push({
                        cbhdId: item.cbhdId,
                        number: item.number,
                        region: item.region,
                        st: item.st,
                        years: item.years,
                        fortification: item.fortification,
                        userName: item.userName,
                        createTime: item.createTime,
                        intensity: item.intensity,
                        damage: item.damage,
                        use: item.use,
                        cs: item.cs,
                    });
                });
            }
        }
    }).catch(err => {
        console.log(err);
    });
};
//重置
const onRest = () => {
    proxy.resetForm("queryRef")
    onSearch();
};
//导入
const onImport = () => {
    router.push({ path: '/disasterDataManagement/importData/buildingDamageData' })
}

// 挂载
onMounted(() => {
    onSearch()
    getProvinceData();
    getUserInfo();
});
</script>

<style lang="scss" scoped>
.paging {
    padding: 20px 0;
}

.form-inline {
    .el-form-item {
        margin-right: 0px;
    }
}
</style>
